package com.evil.enterprise.controller;

import com.evil.application.api.RemoteApplicationReportingDataService;
import com.evil.application.api.RemoteApplicationReportingService;
import com.evil.application.pojo.dto.form.control.ControlBaseInfoDTO;
import com.evil.application.pojo.dto.reporting.*;
import com.evil.application.pojo.dto.reporting.data.ReportingDataReqDTO;
import com.evil.application.pojo.dto.reporting.data.ReportingDataRespDTO;
import com.evil.application.pojo.dto.reporting.data.list.ReportingDataListReqDTO;
import com.evil.common.application.dto.order.OrderPageDataRespDTO;
import com.evil.common.core.entity.R;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 应用报表接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application/reporting")
public class ApplicationReportingController {

    private final RemoteApplicationReportingService remoteApplicationReportingService;

    private final RemoteApplicationReportingDataService remoteApplicationReportingDataService;

    /**
     * 报表枚举
     *
     * @return R
     */
    @GetMapping("/types")
    public R<Map<String, List<?>>> types(@RequestParam("applicationKey") @NotBlank(message = "应用key不能为空") String applicationKey) {
        return R.success(remoteApplicationReportingService.types(applicationKey));
    }

    /**
     * 应用报表基础列表
     *
     * @param appReportingBasesReqDTO appReportingBasesReqDTO
     * @return R
     */
    @PostMapping("/bases")
    public R<List<ApplicationReportingBaseDTO>> appReportingBases(@Validated @RequestBody AppReportingBasesReqDTO appReportingBasesReqDTO) {
        return R.success(remoteApplicationReportingService.appReportingBases(appReportingBasesReqDTO));
    }

    /**
     * 应用报表基础信息
     *
     * @param reportingKey reportingKey
     * @return R
     */
    @PostMapping("/base-info")
    public R<ApplicationReportingBaseDTO> appReportingBaseInfo(@RequestParam("reportingKey") @NotBlank(message = "报表key不能为空") String reportingKey) {
        return R.success(remoteApplicationReportingService.appReportingBaseInfo(reportingKey));
    }

    /**
     * 应用报表信息
     *
     * @param reportingKey reportingKey
     * @return R
     */
    @PostMapping("/info")
    public R<ApplicationReportingDTO> appReportingInfo(@RequestParam("reportingKey") @NotBlank(message = "报表key不能为空") String reportingKey) {
        return R.success(remoteApplicationReportingService.appReportingInfo(reportingKey));
    }

    /**
     * 新增应用报表
     *
     * @param addReportingReqDTO addReportingReqDTO
     * @return R
     */
    @PostMapping("/add")
    public R<AddReportingRespDTO> addReporting(@Validated @RequestBody AddReportingReqDTO addReportingReqDTO) {
        return R.success(remoteApplicationReportingService.addReporting(addReportingReqDTO));
    }

    /**
     * 复制应用报表
     *
     * @param reportingKey reportingKey
     * @return R
     */
    @PostMapping("/copy")
    public R<AddReportingRespDTO> copyReporting(@RequestParam("reportingKey") @NotBlank(message = "报表key不能为空") String reportingKey) {
        return R.success(remoteApplicationReportingService.copyReporting(reportingKey));
    }

    /**
     * 删除应用报表
     *
     * @param reportingKey reportingKey
     * @return R
     */
    @PostMapping("/delete")
    public R<?> deleteReporting(@RequestParam("reportingKey") @NotBlank(message = "报表key不能为空") String reportingKey) {
        remoteApplicationReportingService.deleteReporting(reportingKey);
        return R.success();
    }

    /**
     * 编辑应用报表权限
     *
     * @param modifyReportingPermissionReqDTO modifyReportingPermissionReqDTO
     * @return R
     */
    @PostMapping("/permission/modify")
    public R<AddReportingRespDTO> modifyReportingPermission(@Validated @RequestBody ModifyReportingPermissionReqDTO modifyReportingPermissionReqDTO) {
        remoteApplicationReportingService.modifyReportingPermission(modifyReportingPermissionReqDTO);
        return R.success();
    }

    /**
     * 编辑应用报表
     *
     * @param applicationReportingDTO applicationReportingDTO
     * @return R
     */
    @PostMapping("/modify")
    public R<ApplicationReportingBaseDTO> modifyReporting(@Validated @RequestBody ApplicationReportingDTO applicationReportingDTO) {
        return R.success(remoteApplicationReportingService.modifyReporting(applicationReportingDTO));
    }

    /**
     * 应用报表数据
     *
     * @param reportingDataReqDTO reportingDataReqDTO
     * @return R
     */
    @PostMapping("/data")
    public R<ReportingDataRespDTO> reportingData(@Validated @RequestBody ReportingDataReqDTO reportingDataReqDTO) {
        return R.success(remoteApplicationReportingDataService.reportingData(reportingDataReqDTO));
    }

    /**
     * 应用报表可见控件 基础信息
     *
     * @param reportingKey reportingKey
     * @return R
     */
    @PostMapping("/visible/control")
    public R<List<ControlBaseInfoDTO>> reportingVisibleControlsBases(@RequestParam("reportingKey") @NotBlank(message = "报表key不能为空") String reportingKey) {
        return R.success(remoteApplicationReportingDataService.reportingVisibleControlsBases(reportingKey));
    }

    /**
     * 应用报表 数据表
     *
     * @param reportingDataListReqDTO reportingDataListReqDTO
     * @return R
     */
    @PostMapping("/data/list")
    public R<PageInfo<OrderPageDataRespDTO>> reportingDataList(@Validated @RequestBody ReportingDataListReqDTO reportingDataListReqDTO) {
        return R.success(remoteApplicationReportingDataService.reportingDataList(reportingDataListReqDTO));
    }
}
